package com.example.recyclelibrary;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Eddy on 5/5/17.
 * 自定义旋转的loading
 */

public class CricleLoadingView extends View {

    public static final int DEFAULT_SIZE=30;
    int mIndicatorColor;
    Paint mPaint;
    BaseProgressController mIndicatorController;
    private boolean mHasAnimation;

    public CricleLoadingView(Context context) {
        super(context);
        init(null,0);
    }

    public CricleLoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs,0);
    }

    public CricleLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs,defStyleAttr);
    }

    private void init(AttributeSet attrs,int defStyle){
        TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.CircleLoadingView);
        mIndicatorColor=a.getColor(R.styleable.CircleLoadingView_progress_color, Color.WHITE);
        a.recycle();
        mPaint=new Paint();
        mPaint.setColor(mIndicatorColor);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        applyIndicator();
    }

    public void setProgressbarColor(int color) {
        mIndicatorColor = color;
        mPaint.setColor(mIndicatorColor);
        this.invalidate();
    }

    private void applyIndicator(){
        mIndicatorController=new CircleProgress();
        mIndicatorController.setmTarget(this);
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width=measureDimension(dp2px(DEFAULT_SIZE),widthMeasureSpec);
        int height=measureDimension(dp2px(DEFAULT_SIZE),heightMeasureSpec);
        setMeasuredDimension(width,height);
    }

    private int measureDimension(int defaultSize,int measureSpec){
        int result=defaultSize;
        int specMode=MeasureSpec.getMode(measureSpec);
        int specSize=MeasureSpec.getSize(measureSpec);
        if(specMode==MeasureSpec.EXACTLY){
            result=specSize;
        }else if(specMode==MeasureSpec.AT_MOST){
            result=Math.min(defaultSize,specSize);
        }else {
            result=defaultSize;
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawIndicator(canvas);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        if(!mHasAnimation){
            mHasAnimation=true;
            applyAnimation();
        }
    }

    @Override
    public void setVisibility(int visibility) {
        if(getVisibility()!=visibility){
            super.setVisibility(visibility);
            if(visibility==GONE||visibility==INVISIBLE){
                mIndicatorController.setmAnimationStatus(BaseProgressController.AnimStatus.END);
            }else {
                mIndicatorController.setmAnimationStatus(BaseProgressController.AnimStatus.START);
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mIndicatorController.setmAnimationStatus(BaseProgressController.AnimStatus.CANCLE);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mIndicatorController.setmAnimationStatus(BaseProgressController.AnimStatus.START);
    }



    private void drawIndicator(Canvas canvas){mIndicatorController.draw(canvas,mPaint);}

    private void applyAnimation(){
        mIndicatorController.initAnimation();
    }

    private int dp2px(int dpValue) {
        return (int) getContext().getResources().getDisplayMetrics().density * dpValue;
    }
}
